package com.holiday.springboot2.controller;


import com.holiday.springboot2.task.AsyncTask;
import com.holiday.springboot2.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

@RestController
@RequestMapping("/api/v1/test")
public class TestController {


    @GetMapping("list")
    public JsonData tesetExt() {
        int i = 1 / 0;
        return JsonData.buildSuccess("");
    }

    @Autowired
    private AsyncTask asyncTask;

    @GetMapping("async")
    public JsonData testAsync() throws Exception {
        Future<String> task1 = asyncTask.task1();
        Future<String> task2 = asyncTask.task2();
        for (; ; ) {
            // 判断任务是否执行完毕
            if (task1.isDone() && task2.isDone()) {
                // 获取任务的结果
                String s1 = task1.get();
                System.out.println("s1---"+s1);
                String s2 = task2.get();
                System.out.println("s2---"+s2);
                // 不能一直循环，需要跳出来
                break;
            }
        }
        return JsonData.buildSuccess("xx");
    }
}
